package com.example.api.controller.app;

import com.example.api.dto.AddressDTO;
import com.example.api.exception.ParamException;
import com.example.api.service.CartService;
import com.example.api.utils.CartItem;
import com.example.api.utils.Result;
import com.example.api.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "购物车接口")
@RestController
@RequestMapping("/app/cart")
public class CartController {

    @Resource
    CartService cartService;

    @ApiOperation("获取购物车信息")
    @GetMapping
    public Result list(){
        List<CartItem> cartItemList = cartService.find();
        return Result.ok("查询成功",cartItemList);
    }


    @ApiOperation("加入购物车")
    @PostMapping("/plus")
    public Result plus(int foodId,String flavorItem,String flavorValue){

        //参数校验
        if(foodId<=0){
            throw new ParamException("菜品参数异常");
        }

        //参数校验
            //flavorItem flavorValue要么同时都有值要么同时都没有值，否则就是参数异常

        cartService.plus(foodId,flavorItem,flavorValue);

        return Result.ok("添加成功");
    }

    @ApiOperation("减少购物车")
    @PostMapping("/minus")
    public Result minus(int foodId,String flavorItem,String flavorValue){

        //参数校验
        if(foodId<=0){
            throw new ParamException("菜品参数异常");
        }

        //参数校验
            //flavorItem flavorValue要么同时都有值要么同时都没有值，否则就是参数异常

        cartService.minus(foodId,flavorItem,flavorValue);

        return Result.ok("减少成功");
    }
}
